package dao

import (
	"wish_go/src/main/com/ajie/common"
	"wish_go/src/main/com/ajie/model"
	"wish_go/src/main/com/ajie/vo"
)

func PageCarousel(carousel *model.Carousel, pagination *vo.Pagination) (rows []model.Carousel, total int64, err error) {
	// 分页查询
	offset := (pagination.PageNum - 1) * pagination.PageSize
	db := common.GetDB()
	err = db.Model(&carousel).Where(carousel).Count(&total).Limit(pagination.PageSize).Offset(offset).Order(pagination.SortName).Find(&rows).Error
	return
}

// 页面轮播图
func Carousel() (rows []model.Carousel, err error) {
	// 分页查询
	db := common.GetDB()
	err = db.Model(model.Carousel{}).Limit(10).Offset(-1).Find(&rows).Error
	return
}

func InsertCarousel(carousel model.Carousel) {
	db := common.GetDB()
	// 添加数据
	db.Create(&carousel)
}

func UpdateCarousel(carousel model.Carousel) {
	db := common.GetDB()
	// 更新数据
	db.Model(&carousel).Select("url", "sort", "update_name").Updates(carousel)
}

func DeleteCarousel(carousel model.Carousel) {
	db := common.GetDB()
	// 删除数据。根据主键删除--存在deleted_at 就软删除
	db.Delete(&carousel)
}
